package com.zh.rent.controller.backstage;

import com.github.pagehelper.PageInfo;
import com.zh.rent.pojo.User;
import com.zh.rent.service.IUserService;
import com.zh.rent.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Admin")
@Api(tags="用户后台管理")
public class AdminController {
    @Autowired
    IUserService IUserServicerImpl;

    @PostMapping("/log")
    @ResponseBody
    @ApiOperation(value = "后台登录")
    public JsonResult log(String name, String pwd, HttpServletRequest request) {
        User user = new User();
        user.setUserpwd(pwd);
        user.setUserName(name);
        int i = IUserServicerImpl.selUser(user);
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(15*60);  //设置过期时间以秒为单位，会自动刷新
        session.setAttribute("i",i);
        Object ob=session.getAttribute("preurl");
        if(i==0){
            return JsonResult.ERROR("登录失败");
        }
        return JsonResult.OK("登录成功");
    }

    @PostMapping("/adminAll")
    @ResponseBody
    @ApiOperation(value = "用户表分页查询")
    public JsonResult adminAll(@RequestParam(value="pageSize",defaultValue="3")Integer pageSize,
                               @RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
                               String name) {
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        PageInfo<User> userPageInfo = IUserServicerImpl.selUserAll(pageNum, pageSize, map);
        return JsonResult.OK(userPageInfo);
    }

    @PostMapping("/adminUpintset")
    @ResponseBody
    @ApiOperation(value = "用户表增加")
    public JsonResult adminUpintset(String name, String pwd) {
        try {
            User user = new User();
            user.setUserpwd(pwd);
            user.setUserName(name);
            Integer integer = IUserServicerImpl.intsetUser(user);
            if (integer != 0 && integer != null) {
                return JsonResult.OK("增加成功");
            }else {
                return JsonResult.ERROR("失败");
            }
        }catch (Exception e){
            return JsonResult.ERROR("失败错误信息："+e.getMessage());
        }
    }

    @PostMapping("/adminUpdate")
    @ResponseBody
    @ApiOperation(value = "用户表修改")
    public JsonResult adminUpdate(int userid, String name, String pwd) {
        try {
            User user = new User();
            user.setUserid(userid);
            user.setUserName(name);
            user.setUserpwd(pwd);
            int i = IUserServicerImpl.updateNum(user);
            return JsonResult.OK("修改成功");
        }catch (Exception e){
            return JsonResult.ERROR("失败错误信息："+e.getMessage());
        }
    }

    @PostMapping("/admindelAll")
    @ResponseBody
    @ApiOperation(value = "用户表根据id批量删除")
    public JsonResult admindelAll(String array) {
        try {
            String buy= array.substring(1,array.length()-1);
            List<Integer> list = new ArrayList<Integer>();
            String[] str = buy.split(",");
            for (int i = 0; i < str.length; i++) {
                int i1 = Integer.parseInt(str[i]);
                list.add(i1);
            }
            int deluser = IUserServicerImpl.deluserAll(list);
            if (deluser != 0) {
                return JsonResult.OK("批量删除成功");
            } else {
                return JsonResult.ERROR("批量删除失败");
            }
        }catch (Exception e){
            return JsonResult.ERROR("批量删除失败");
        }
    }

    @PostMapping("/admindel")
    @ResponseBody
    @ApiOperation(value = "用户表根据id删除")
    public JsonResult admindel(int userid) {
        try {
            int deluser = IUserServicerImpl.deluser(userid);
            if (deluser != 0) {
                return JsonResult.OK("批量删除成功");
            } else {
                return JsonResult.ERROR("批量删除失败");
            }
        }catch (Exception e){
            return JsonResult.ERROR("批量删除失败");
        }
    }



}
